<?php


$plugin_info = array(
						'pi_name'			=> 'Iain Future Monthlee',
						'pi_version'		=> '1.0',
						'pi_author'			=> 'Iain Urquhart',
						'pi_author_url'		=> 'http://www.iain.co.nz',
						'pi_description'	=> 'Displays a list of future months containing entries, and count totals',
						'pi_usage'			=> Iain_future_monthlee::usage()
					);
					
class Iain_future_monthlee {

	var $return_data;
	
	function Iain_future_monthlee() {
		$this->show_list();
	}
	
	function show_list() {
		global $DB, $TMPL;
				
		$months = array("", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
		
		$weblog = (!$TMPL->fetch_param('weblog')) ? "default_weblog" : $TMPL->fetch_param('weblog');
		$limit = (!$TMPL->fetch_param('limit')) ? "12" : $TMPL->fetch_param('limit');
		$path = (!$TMPL->fetch_param('path')) ? "" : $TMPL->fetch_param('path');
		$segcheck = (!$TMPL->fetch_param('segcheck')) ? "" : $TMPL->fetch_param('segcheck');

		$sql = "(SELECT DISTINCT month, year
					FROM exp_weblog_titles
					WHERE status='open' AND entry_date > '".time()."'
					GROUP BY month, year
					ORDER BY year ASC, month ASC";
					
		if (is_numeric($limit)) $sql .= " LIMIT $limit";
		
		$sql .= ") ORDER BY year ASC, month ASC";

		$query = $DB->query($sql);
		
		// $monthcount = $query->num_rows;
				
		$r = "<ul>";
		
		foreach($query->result as $row) {
			$month = $row['month'];
			$year = $row['year'];
			
			$postq = "SELECT COUNT(month) AS count
								FROM exp_weblog_titles
								WHERE month='$month' AND year='$year'
								AND status='open' AND entry_date > '".time()."'";
								
			$postr = $DB->query($postq);
			$count = $postr->row['count'];
			
			$thischeck = $year.$month;
			
			if($segcheck == $thischeck) {
				$active = " class='selected'";
			}
			$r .= "<li".$active."><a href='".$path.$year."/".$month."'".$active."><span>".$months[intval($month)]." ".$year."</span> &mdash; (".$count.")</a></li>";
			$active ='';
		}
		
		$r .= "</ul>";
		
		$this->return_data = $r;
		
	}
	
	// ----------------------------------------
	//  Plugin Usage
	// ----------------------------------------
	
	function usage() {
		ob_start(); 
		?>

		Example:
		
		{exp:Iain_future_monthlee weblog="events" limit="12" path="/index.php/events/archives/" segcheck="{segment_3}{segment_4}"}
		
		parameters are:
		
		weblog=""
			weblog="default_weblog" (default)
					
		limit=""
			limit="12"
			How many months to show that contain entries
			
		path=
			where your archive template is located, eg: path="/index.php/events/archives/"
			The plugin will append '2009/08' for example.
			
		segcheck=
				location of the year and month in your url
				segcheck="{segment_3}{segment_4}" would be used for applying class="selected" while browsing '/events/archives/2009/08'


		<?php
		$buffer = ob_get_contents();
			
		ob_end_clean(); 
		
		return $buffer;
	}
}
?>